#include <SoftwareSerial.h>
#include <CapacitiveSensor.h>
 
SoftwareSerial BTSerial(3,4);

CapacitiveSensor   cs_2_7 = CapacitiveSensor(2,7); 
CapacitiveSensor   cs_2_12 = CapacitiveSensor(2,12);
CapacitiveSensor   cs_2_8 = CapacitiveSensor(2,8);
CapacitiveSensor   cs_2_13 = CapacitiveSensor(2,13); 

long  total_sum =0; 
char goal_sum =0;
long before_sum =0;

void setup() 
{
  Serial.begin(9600);
  Serial.write("Connect PC\n");
  
  BTSerial.begin(9600);
  Serial.write("Connect BlooTooth\n");
}
void loop()
{
   if (BTSerial.available())
  {
    if('R' == (char)BTSerial.read() || 'r' == (char)BTSerial.read()) 
    {
      Serial.write("sum reset.");
    }
    goal_sum = (char)BTSerial.read();
    
  }
  
    long coin_10 = cs_2_7.capacitiveSensor(30);
    long coin_50 = cs_2_12.capacitiveSensor(30);
    long coin_100 = cs_2_8.capacitiveSensor(30);  
    long coin_500 = cs_2_13.capacitiveSensor(30);
    long coin = 0;

    before_sum = total_sum ;
    
    if(coin_10 != 0)
    {
       coin = 10;
    }
    else if(coin_50 != 0)
    {
       coin = 50;
    }
    else if(coin_100 != 0)
    {
       coin = 100;
    }
    else if(coin_500 != 0)
    {
       coin = 500;
    } 
    
  total_sum += coin;
  
  if (total_sum != before_sum)
  {
  Serial.print("total_sum: ");
  Serial.println(total_sum);
  }
  
  if(goal_sum != 0)
   {
      if ( (char)total_sum >= goal_sum )
      {
         Serial.println("Success! ");
         Serial.print(" Goal_sum : ");
         Serial.println(goal_sum);
         Serial.print(" Total sum: ");
         Serial.println(total_sum);
         
         return ;
      }
   }
   
  delay(160); 
  
}


